package com.ztools.pc.config;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.ztools.common.model.pc.sys.User;
import com.ztools.common.util.pc.RedisUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author:一口香菜
 * @Date: 2023/3/1  20:39
 * @Version 1.0
 * mybatisPlus插件使用
 */
@Configuration
public class MybatisConfig implements MetaObjectHandler {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 乐观锁配置
     *
     * @return 插件
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        //1 创建MybatisPlusInterceptor拦截器对象
        MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
        //2 添加乐观锁拦截器
        mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mpInterceptor;
    }

    /**
     * 分页插件。如果你不配置，分页插件将不生效
     */
    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 指定数据库方言为 MYSQL
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 添加自动填充
     *
     * @param metaObject 数据源
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        // 获取当前登入人员
        String user = stringRedisTemplate.opsForValue().get(RedisUtil.CURRUSER);
        User sysUser = JSONUtil.toBean(user, User.class);
        this.setFieldValByName("modifiedDate", new Date(), metaObject);
        this.setFieldValByName("createdDate", new Date(), metaObject);
        this.setFieldValByName("modifierName", sysUser.getLoginName(), metaObject);
        this.setFieldValByName("modifierId", sysUser.getId(), metaObject);
        this.setFieldValByName("creatorName", sysUser.getLoginName(), metaObject);
        this.setFieldValByName("creatorId", sysUser.getId(), metaObject);
    }

    /**
     * 添加自动修改
     *
     * @param metaObject 数据源
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        // 获取当前登入人员
        String user = stringRedisTemplate.opsForValue().get(RedisUtil.CURRUSER);
        User sysUser = JSONUtil.toBean(user, User.class);
        this.setFieldValByName("modifiedDate", new Date(), metaObject);
        this.setFieldValByName("modifierName", sysUser.getLoginName(), metaObject);
        this.setFieldValByName("modifierId", sysUser.getId(), metaObject);
    }

}
